package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.ContactsAdapter;
import flc.ast.adapter.ContactsRecordAdapter;
import flc.ast.adapter.PictureRecordAdapter;
import flc.ast.adapter.SelPictureAdapter;
import flc.ast.adapter.SelVideoAdapter;
import flc.ast.adapter.VideoRecordAdapter;
import flc.ast.bean.MyRecRecordBean;
import flc.ast.bean.SelectFileBean;
import flc.ast.databinding.ActivityTransferRecordBinding;
import flc.ast.utils.AppSpUtils;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.bean.ContactInfo;
import stark.common.basic.utils.ContactUtil;
import stark.common.basic.utils.RxUtil;

public class TransferRecordActivity extends BaseAc<ActivityTransferRecordBinding> {

    private PictureRecordAdapter pictureAdapter;
    private VideoRecordAdapter videoAdapter;
    private ContactsRecordAdapter contactsAdapter;
    private int type = 0;
    private boolean isPicAll = false, isVideoAll = false, isContacts = false;
    private List<SelectFileBean> listSel = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.activity_transfer_record;
    }

    @Override
    protected void initView() {
        mDataBinding.ivTransferRecordBack.setOnClickListener(v -> onBackPressed());
        //图片
        mDataBinding.rvPictureList.setLayoutManager(new GridLayoutManager(mContext, 3));
        pictureAdapter = new PictureRecordAdapter();
        mDataBinding.rvPictureList.setAdapter(pictureAdapter);
        pictureAdapter.setOnItemClickListener(this);
        //视频
        mDataBinding.rvVideoList.setLayoutManager(new GridLayoutManager(mContext, 3));
        videoAdapter = new VideoRecordAdapter();
        mDataBinding.rvVideoList.setAdapter(videoAdapter);
        videoAdapter.setOnItemClickListener(this);
        //通讯录
        mDataBinding.rvContactsList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        contactsAdapter = new ContactsRecordAdapter();
        mDataBinding.rvContactsList.setAdapter(contactsAdapter);
        contactsAdapter.setOnItemClickListener(this);

        mDataBinding.tvPicture.setOnClickListener(this);
        mDataBinding.tvVideo.setOnClickListener(this);
        mDataBinding.tvContacts.setOnClickListener(this);
        mDataBinding.tvAllSel.setOnClickListener(this);
        mDataBinding.tvDelete.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvPicture://图片
                clearSelection();
                mDataBinding.tvPicture.setTextColor(Color.parseColor("#FFFFFF"));
                mDataBinding.tvPicture.setBackgroundResource(R.drawable.shape_language_sel);
                type = 0;
                if (pictureAdapter.getValidData() != null) {
                    mDataBinding.rvPictureList.setVisibility(View.VISIBLE);
                } else {
                    mDataBinding.tvFileNoData.setVisibility(View.VISIBLE);
                }
                if (isPicAll) {
                    mDataBinding.tvAllSel.setText(getString(R.string.cancel_all_sel));
                } else {
                    mDataBinding.tvAllSel.setText(getString(R.string.all_sel));
                }
                break;
            case R.id.tvVideo://视频
                clearSelection();
                mDataBinding.tvVideo.setTextColor(Color.parseColor("#FFFFFF"));
                mDataBinding.tvVideo.setBackgroundResource(R.drawable.shape_language_sel);
                type = 1;
                if (videoAdapter.getValidData() != null) {
                    mDataBinding.rvVideoList.setVisibility(View.VISIBLE);
                } else {
                    mDataBinding.tvFileNoData.setVisibility(View.VISIBLE);
                }
                if (isVideoAll) {
                    mDataBinding.tvAllSel.setText(getString(R.string.cancel_all_sel));
                } else {
                    mDataBinding.tvAllSel.setText(getString(R.string.all_sel));
                }
                break;
            case R.id.tvContacts://通讯录
                clearSelection();
                mDataBinding.tvContacts.setTextColor(Color.parseColor("#FFFFFF"));
                mDataBinding.tvContacts.setBackgroundResource(R.drawable.shape_language_sel);
                type = 2;
                if (contactsAdapter.getValidData() != null) {
                    mDataBinding.rvContactsList.setVisibility(View.VISIBLE);
                } else {
                    mDataBinding.tvFileNoData.setVisibility(View.VISIBLE);
                }
                if (isContacts) {
                    mDataBinding.tvAllSel.setText(getString(R.string.cancel_all_sel));
                } else {
                    mDataBinding.tvAllSel.setText(getString(R.string.all_sel));
                }
                break;
            case R.id.tvAllSel://全选
                switch (type) {
                    case 0://图片
                        clearPicData();
                        if (isPicAll) {
                            getPicData();
                            isPicAll = false;
                            mDataBinding.tvAllSel.setText(getString(R.string.all_sel));
                        } else {
                            addAllPic();
                            isPicAll = true;
                            mDataBinding.tvAllSel.setText(getString(R.string.cancel_all_sel));
                        }
                        break;
                    case 1://视频
                        clearVideoData();
                        if (isVideoAll) {
                            getVideoData();
                            isVideoAll = false;
                            mDataBinding.tvAllSel.setText(getString(R.string.all_sel));
                        } else {
                            addAllVideo();
                            isVideoAll = true;
                            mDataBinding.tvAllSel.setText(getString(R.string.cancel_all_sel));
                        }
                        break;
                    case 2://联系人
                        clearContactsData();
                        if (isContacts) {
                            getContactsData();
                            isContacts = false;
                            mDataBinding.tvAllSel.setText(getString(R.string.all_sel));
                        } else {
                            addAllContacts();
                            isContacts = true;
                            mDataBinding.tvAllSel.setText(getString(R.string.cancel_all_sel));
                        }
                        break;
                }
                isNotSel();
                break;
            case R.id.tvDelete://删除
                if (listSel.size() == 0) {
                    ToastUtils.showShort(R.string.please_sel_record);
                    return;
                }
                List<SelectFileBean> listSp = AppSpUtils.getTransferRecordList();
                for (SelectFileBean bean : listSel) {
                    for (SelectFileBean selectFileBean : listSp) {
                        if (selectFileBean.getPath().equals(bean.getPath())) {
                            listSp.remove(selectFileBean);
                            break;
                        }
                    }
                }
                AppSpUtils.saveTransferRecordList(listSp);
                listSel.clear();
                getPicData();
                getVideoData();
                getContactsData();
                isNotSel();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter == pictureAdapter) {
            if (pictureAdapter.getItem(position).isSelected()) {
                pictureAdapter.getItem(position).setSelected(false);
                listSel.remove(pictureAdapter.getItem(position));
            } else {
                listSel.add(pictureAdapter.getItem(position));
                pictureAdapter.getItem(position).setSelected(true);
            }
        } else if (adapter == videoAdapter) {
            if (videoAdapter.getItem(position).isSelected()) {
                videoAdapter.getItem(position).setSelected(false);
                listSel.remove(videoAdapter.getItem(position));
            } else {
                listSel.add(videoAdapter.getItem(position));
                videoAdapter.getItem(position).setSelected(true);
            }
        } else if (adapter == contactsAdapter) {
            if (contactsAdapter.getItem(position).isSelected()) {
                contactsAdapter.getItem(position).setSelected(false);
                listSel.remove(contactsAdapter.getItem(position));
            } else {
                listSel.add(contactsAdapter.getItem(position));
                contactsAdapter.getItem(position).setSelected(true);
            }
        }
        adapter.notifyDataSetChanged();
        isNotSel();
    }

    @Override
    protected void initData() {
        getPicData();
        getVideoData();
        getContactsData();
        if (pictureAdapter.getValidData() != null) {
            mDataBinding.rvPictureList.setVisibility(View.VISIBLE);
        } else {
            mDataBinding.tvFileNoData.setVisibility(View.VISIBLE);
        }
    }

    //获取视频和图片数据
    private void getPicData() {
        List<SelectFileBean> listSp = AppSpUtils.getTransferRecordList();
        List<SelectFileBean> listShow = new ArrayList<>();
        if (listSp != null && listSp.size() > 0) {
            for (SelectFileBean bean : listSp) {
                if (bean.getType() == 1) {
                    listShow.add(bean);
                }
            }
            pictureAdapter.setList(listShow);
        }
    }

    //获取视频和图片数据
    private void getVideoData() {
        List<SelectFileBean> listSp = AppSpUtils.getTransferRecordList();
        List<SelectFileBean> listShow = new ArrayList<>();
        if (listSp != null && listSp.size() > 0) {
            for (SelectFileBean bean : listSp) {
                if (bean.getType() == 2) {
                    listShow.add(bean);
                }
            }
            videoAdapter.setList(listShow);
        }
    }

    //获取联系人列表
    private void getContactsData() {
        List<SelectFileBean> listSp = AppSpUtils.getTransferRecordList();
        List<SelectFileBean> listShow = new ArrayList<>();
        if (listSp != null && listSp.size() > 0) {
            for (SelectFileBean bean : listSp) {
                if (bean.getType() == 3) {
                    listShow.add(bean);
                }
            }
            contactsAdapter.setList(listShow);
        }
    }

    //判断是否没有选择
    private void isNotSel() {
        if (listSel.size() != 0) {
            mDataBinding.tvDelete.setTextColor(Color.parseColor("#B3313131"));
        } else {
            mDataBinding.tvDelete.setTextColor(Color.parseColor("#4D313131"));
        }
    }

    //清空选中图片
    private void clearPicData() {
        Iterator<SelectFileBean> iterator = listSel.iterator();
        while (iterator.hasNext()) {
            SelectFileBean bean = iterator.next();
            if (bean.getType() == 1) {
                iterator.remove();
            }
        }
    }

    //全选视频
    private void addAllPic() {
        for (SelectFileBean validDatum : pictureAdapter.getValidData()) {
            listSel.add(validDatum);
            validDatum.setSelected(true);
        }
        pictureAdapter.notifyDataSetChanged();
    }

    //清空选中视频
    private void clearVideoData() {
        Iterator<SelectFileBean> iterator = listSel.iterator();
        while (iterator.hasNext()) {
            SelectFileBean bean = iterator.next();
            if (bean.getType() == 2) {
                iterator.remove();
            }
        }
    }

    //全选图片
    private void addAllVideo() {
        for (SelectFileBean validDatum : videoAdapter.getValidData()) {
            listSel.add(validDatum);
            validDatum.setSelected(true);
        }
        videoAdapter.notifyDataSetChanged();
    }

    //清空选中联系人
    private void clearContactsData() {
        Iterator<SelectFileBean> iterator = listSel.iterator();
        while (iterator.hasNext()) {
            SelectFileBean bean = iterator.next();
            if (bean.getType() == 3) {
                iterator.remove();
            }
        }
    }

    //全选联系人
    private void addAllContacts() {
        for (SelectFileBean validDatum : contactsAdapter.getValidData()) {
            listSel.add(validDatum);
            validDatum.setSelected(true);
        }
        contactsAdapter.notifyDataSetChanged();
    }

    private void clearSelection() {
        mDataBinding.tvPicture.setTextColor(Color.parseColor("#80313131"));
        mDataBinding.tvPicture.setBackgroundResource(0);
        mDataBinding.tvVideo.setTextColor(Color.parseColor("#80313131"));
        mDataBinding.tvVideo.setBackgroundResource(0);
        mDataBinding.tvContacts.setTextColor(Color.parseColor("#80313131"));
        mDataBinding.tvContacts.setBackgroundResource(0);
        mDataBinding.rvPictureList.setVisibility(View.GONE);
        mDataBinding.rvVideoList.setVisibility(View.GONE);
        mDataBinding.rvContactsList.setVisibility(View.GONE);
        mDataBinding.tvFileNoData.setVisibility(View.GONE);
    }

}